package com.wxdc.nmgyt.nmgytwydc.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.model.LatLng;
import com.esri.android.map.GraphicsLayer;
import com.esri.core.geometry.Point;
import com.wxdc.nmgyt.nmgytwydc.R;

import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by Administrator on 2018/4/9.
 */

public class SaveGpsDataUtil {
    private final String TAG = this.getClass().getSimpleName();
    private Context mContext;
    public SaveGpsDataUtil(Context context) {
        mContext = context;
    }

    public void saveGpsDataInfoByGis(List<Point> pointList, GraphicsLayer graphicsLayer) {//保存GPS采集数据
        //1.弹出对话框,输入文件名
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("输入文件名");
        builder.setIcon(R.drawable.rename_dialog_title_icon);
        View reNameView = LayoutInflater.from(mContext).inflate(R.layout.rename_dialog, null);
        final EditText reNameEdit = (EditText) reNameView.findViewById(R.id.rename_dialog_filename);
        builder.setView(reNameView);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.setPositiveButton("保存", null);
        AlertDialog dialog =  builder.create();
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(reNameEdit.getText().toString().trim())) {
                    reNameEdit.setHint("请输入文件名称");
                    return;
                }

                //1.定义文件
                FileUtil fileUtil = new FileUtil(FileUtil.GpsDataFile);
                FileOutputStream fos = fileUtil.getFileOutputstream(reNameEdit.getText().toString().trim());
                //2.将获得的点数据存入文件
                int len = pointList.size();
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < len; i++) {
                    stringBuilder.append(pointList.get(i).getX()+ "," + pointList.get(i).getY() + "\r\n");
                    Log.i(TAG, "onClick: ----------"+pointList.get(i).getX());
                }
                fileUtil.writeFile(fos, stringBuilder.toString());
                //3.关闭对话框
                dialog.dismiss();
                Toast.makeText(mContext,"保存成功",Toast.LENGTH_SHORT).show();
                fileUtil.close(fos);
                pointList.clear();
                graphicsLayer.removeAll();
            }
        });
    }
    public void saveGpsDataInfoByBaidu(List<LatLng> mLatLngs, BaiduMap baiduMap) {//保存GPS采集数据
        //1.弹出对话框,输入文件名
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("输入文件名");
        builder.setIcon(R.drawable.rename_dialog_title_icon);
        View reNameView = LayoutInflater.from(mContext).inflate(R.layout.rename_dialog, null);
        final EditText reNameEdit = (EditText) reNameView.findViewById(R.id.rename_dialog_filename);
        builder.setView(reNameView);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.setPositiveButton("保存", null);
        AlertDialog dialog =  builder.create();
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(reNameEdit.getText().toString().trim())) {
                    reNameEdit.setHint("请输入文件名称");
                    return;
                }
                //1.定义文件
                FileUtil fileUtil = new FileUtil(FileUtil.GpsDataFile);
                FileOutputStream fos = fileUtil.getFileOutputstream(reNameEdit.getText().toString().trim());
                //2.将获得的点数据存入文件
                int len = mLatLngs.size();
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < len; i++) {
                    stringBuilder.append(mLatLngs.get(i).longitude + "," + mLatLngs.get(i).latitude + "\r\n");
                }
                fileUtil.writeFile(fos, stringBuilder.toString());
                //3.关闭对话框
                dialog.dismiss();
                Toast.makeText(mContext,"保存成功",Toast.LENGTH_SHORT).show();
                fileUtil.close(fos);
                baiduMap.clear();
                mLatLngs.clear();
            }
        });
        /// 小结:在创建AlertDialog时setPositiveButton方法的OnClickListener参数需传入null，然后让dialog show出来以后，再通过getButton(AlertDialog.BUTTON_POSITIVE)方法重新得到确定按钮，重设点击事件，这时如果不手动去调dialog.dismiss()，对话框就不会消失了
        ///注：必须要先调show()方法，后面的getButton才有效
    }

}
